from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SKU, GoodsCategory, SPU, SPUSpecification
from sadmin.serializersss.sku_serializers import SKUModelSerializers, GoodsCategorySerializer, SPUSimpleSerializer, \
    SPUSpecSerializer
from sadmin.utils import Mypage


class SKUSetView(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUModelSerializers
    pagination_class =Mypage
    def get_queryset(self):
        data = self.request.query_params.get("keyword")
        if data:
            queryset = SKU.objects.filter(name__contains=data)
            return queryset
        return SKU.objects.all()

class SKUCategorieView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    serializer_class = GoodsCategorySerializer


class SPUSimpleView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

class SPUSpecOptView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SPUSpecSerializer

    def get_queryset(self):
        # 1、提取前端传来的spu_id
        spu_id = self.kwargs.get("pk")
        # 2、过滤出关联spu_id的所有的SPUSpecification对象
        # 3、返回过滤后的数据集
        return self.queryset.filter(spu_id=spu_id)